clc;
clear all;
%% Set PWM Switching frequency
PWM_frequency 	= 20e3;    %Hz          // converter s/w freq
T_pwm           = 1/PWM_frequency;  %s  // PWM switching time period

%% Set Sample Times
Ts          	= T_pwm;        %sec        // Sample time step for controller

%% Set data type for controller & code-gen
dataType = 'single';            % Floating point code-generation

pmsm.model             ='DJI-3508';
pmsm.sn                ='0';
pmsm.p                 =7;
pmsm.Rs                =0.194;
pmsm.Ld                =0.097e-03;
pmsm.Lq                =0.097e-03;
pmsm.Ls                =pmsm.Ld;
pmsm.J                 =1.725094E-08;
pmsm.B                 =2.6715e-8;
pmsm.N_max             =20000;
pmsm.Ke                =0.272;  % //这里并不是反电动势常数，而是在某个RPM情况下，测量AB相的PEAK值，Vpp/2
pmsm.FluxPM            =(pmsm.Ke)/(sqrt(3)*2*pi*1000*pmsm.p/60);
pmsm.FluxPM = 0.001052;
pmsm.Kt                =(3/2)*pmsm.FluxPM*pmsm.p;
pmsm.I_rated           =1.2;
pmsm.N_base            =12540;
pmsm.F_base            =pmsm.N_base*pmsm.p/60;
pmsm.DC                =12;
pmsm.T_rated           = (3/2)*pmsm.p*pmsm.FluxPM*pmsm.I_rated;

Vmin                   =0.001;


Kp_C=pmsm.Lq/(4*Ts);
Ki_C=pmsm.Rs/(4*Ts);

Kp_S=(pi*pmsm.J)/(45*4*Ts*10^(2.5/2)*pmsm.p*pmsm.FluxPM);
Ki_S=(pi*pmsm.J)/(45*4*Ts*10^(2.5/2)*pmsm.p*pmsm.FluxPM*4*Ts*10^(2.5));


% Interval
inverter.model         = 'X-NUCLEO-IHM07M1'; 	% 		// Manufacturer Model Number
inverter.sn            = 'INV_XXXX';         	% 		// Manufacturer Serial Number
inverter.V_dc          = 12;       				%V      // DC Link Voltage of the Inverter
inverter.I_trip        = 10;       				%Amps   // Max current for trip
inverter.Rds_on        = 2e-3;     				%Ohms   // Rds ON for X-NUCLEO-IHM07M1
inverter.Rshunt        = 0.33;    				%Ohms   // Rshunt for X-NUCLEO-IHM07M1
inverter.CtSensAOffset = 1850;        			%Counts // ADC Offset for phase-A
inverter.CtSensBOffset = 1857;        			%Counts // ADC Offset for phase-B
inverter.ADCGain       = 1;                     %       // ADC Gain factor scaled by SPI
inverter.EnableLogic   = 1;    					% 		// Active high for X-NUCLEO-IHM07M1 enable pin (EN_GATE)
inverter.invertingAmp  = -1;   					% 		// Currents entering motor phases are read as negative values in this hardware
inverter.ISenseVref    = 3.3;					%V 		// Voltage ref of inverter current sense circuit
inverter.ISenseVoltPerAmp = 0.505; 				%V/Amps // Current sense voltage output per 1 A current (Rshunt * iSense op-amp gain)
inverter.ISenseMax     = inverter.ISenseVref/(2*inverter.ISenseVoltPerAmp); %Amps // Maximum Peak-Neutral current that can be measured by inverter current sense
inverter.R_board        = inverter.Rds_on + inverter.Rshunt/3;  %Ohms

SMO.MaxSMCError=1.2;
SMO.Kslide=0.65;
SMO.OffsetAngle=0.52;

OPL.Speed=1200;
OPL.Time=3;
OPL.Iq=0.4;


LGB.OMEGA = pmsm.N_max * 2*pi*7/60;
LBG.K1=pmsm.Rs/ pmsm.Ls-2*LGB.OMEGA  *1;%w max 取值11000 为rps15000
LBG.K2=LGB.OMEGA * LGB.OMEGA*pmsm.Ls;
LBG.h=0.1;
LBG.OffsetAngle=0.1;

gamma = 1E10;

%to pll
Gs_flux = tf(1,[1,0]);
Gs_z_flux = c2d(Gs_flux,T_pwm,'zoh');

Gs_cur = tf(1,[pmsm.Ld,pmsm.Rs]);
Gs_z_cur = c2d(Gs_cur,T_pwm,'zoh');

